package com.example.roomjavademo.repository;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import androidx.lifecycle.LiveData;

import com.example.roomjavademo.dao.UserDao;
import com.example.roomjavademo.database.DatabaseHelper;
import com.example.roomjavademo.entity.UserBean;

import java.util.List;

/**
 * 数据仓库类
 */
public class UserRepository {
    private LiveData<List<UserBean>> mListLiveData;
    private final UserDao mUserDao;
    private Context mContext;

    public UserRepository(Context context) {
        mContext = context;
        DatabaseHelper databaseHelper = DatabaseHelper.getDataBase(context.getApplicationContext());
        mUserDao = databaseHelper.getUserDao();
        mListLiveData = mUserDao.getAll();
    }

    public LiveData<List<UserBean>> getListLiveData() {
        return mListLiveData;
    }

    public void insert(UserBean... userBeans) {
        new InsertAsyncTask(mUserDao, mContext).execute(userBeans);
    }

    public void delete(UserBean... userBeans) {
        new DeleteAsyncTask(mUserDao, mContext).execute(userBeans);
    }

    public void deleteAll() {
        new DeleteAllAsyncTask(mUserDao, mContext).execute();
    }

    public void update(UserBean... userBeans) {
        new UpdateAsyncTask(mUserDao, mContext).execute(userBeans);
    }

    private static class InsertAsyncTask extends AsyncTask<UserBean, Void, Long[]> {
        private UserDao mUserDao;
        private Context mContext;

        public InsertAsyncTask(UserDao userDao, Context context) {
            mUserDao = userDao;
            mContext = context;
        }

        @Override
        protected Long[] doInBackground(UserBean... userBeans) {

            return this.mUserDao.insertUser(userBeans);
        }

        @Override
        protected void onPostExecute(Long[] longs) {
            if (longs.length > 0) {
                Toast.makeText(mContext, "新增成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private static class DeleteAsyncTask extends AsyncTask<UserBean, Void, Integer> {
        private UserDao mUserDao;
        private Context mContext;

        public DeleteAsyncTask(UserDao userDao, Context context) {
            mUserDao = userDao;
            mContext = context;
        }

        @Override
        protected Integer doInBackground(UserBean... userBeans) {

            return this.mUserDao.deleteUser(userBeans);
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (integer > 0) {
                Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Integer> {
        private UserDao mUserDao;
        private Context mContext;

        public DeleteAllAsyncTask(UserDao userDao, Context context) {
            mUserDao = userDao;
            mContext = context;
        }


        @Override
        protected Integer doInBackground(Void... voids) {
            return this.mUserDao.deleteAll();
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (integer > 0) {
                Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private static class UpdateAsyncTask extends AsyncTask<UserBean, Void, Integer> {
        private UserDao mUserDao;
        private Context mContext;

        public UpdateAsyncTask(UserDao userDao, Context context) {
            mUserDao = userDao;
            mContext = context;
        }

        @Override
        protected Integer doInBackground(UserBean... userBeans) {

            return this.mUserDao.updateUser(userBeans);
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (integer > 0) {
                Toast.makeText(mContext, "更新成功", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
